package pfc.pfcjoc.entities;

import static playn.core.PlayN.log;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.Fixture;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

import pfc.pfcjoc.core.pfcjoc;
import playn.core.GroupLayer;
import playn.core.Layer;

public class Toad extends PhysicElement {
  public static String name = "toad";
  private int timeSinceLastSprite = 0;

  public static final float MAX_SPEED = 1.5f;

  private int numSpritesRunAnim = 2;

  private int numEsqContacts = 0;
  private int numDretaContacts = 0;

  private boolean movingLeft = true;
  private boolean movingRight = false;


  public Toad(final World world, final GroupLayer worldLayer, final Vec2 pos) {
    super(world, worldLayer, pos);
    setDepth(200);
    initPhysics(world, pos);
  }


  public void initPhysics(World world, Vec2 pos) {
    position = pos;
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DYNAMIC;
    bodyDef.position = pos;
    bodyDef.fixedRotation = true;
    bodyDef.linearDamping = 0.0f;
    body = world.createBody(bodyDef);

    // PolygonShape boxShape = new PolygonShape();
    // boxShape.setAsBox(w / 2f, h / 2f);

    float w = pfcjoc.toPhy(getWidth());
    float h = pfcjoc.toPhy(getHeight());

    CircleShape boxShape = new CircleShape();
    boxShape.m_radius = w / 2f;

    FixtureDef fxtDefToadBody = new FixtureDef();
    fxtDefToadBody.shape = boxShape;
    fxtDefToadBody.density = 1.5f;
    fxtDefToadBody.friction = 1.0f;
    fxtDefToadBody.restitution = 0.0f;
    Fixture toadBodyFxtFixture = body.createFixture(fxtDefToadBody);
    toadBodyFxtFixture.setUserData("toadBody");
    body.setLinearDamping(0.1f);

    // Sensor a esquerra i dreta per saber quan xoca amb algun altre objecte
    // Sensor esquerra
    PolygonShape sensorEsqShape = new PolygonShape();
    sensorEsqShape.setAsBox(0.005f, h / 3f, new Vec2(0 - w / 2f + 0.005f / 2, 0), 0);

    FixtureDef fxtDefSensorEsq = new FixtureDef();
    fxtDefSensorEsq.isSensor = true;
    fxtDefSensorEsq.shape = sensorEsqShape;

    Fixture sensorEsqFxt = body.createFixture(fxtDefSensorEsq);
    sensorEsqFxt.setUserData("sensorEsqToad");

    // Sensor dreta
    PolygonShape sensorDretaShape = new PolygonShape();
    sensorDretaShape.setAsBox(0.005f, h / 3f, new Vec2(0 + w / 2f + 0.005f / 2, 0), 0);

    FixtureDef fxtDefSensorDreta = new FixtureDef();
    fxtDefSensorDreta.isSensor = true;
    fxtDefSensorDreta.shape = sensorDretaShape;

    Fixture sensorDretaFxt = body.createFixture(fxtDefSensorDreta);
    sensorDretaFxt.setUserData("sensorDretaToad");

    // Sensor cap
    PolygonShape sensorCapShape = new PolygonShape();
    // TODO:
    sensorCapShape.setAsBox(w / 2f, 0.005f, new Vec2(0, 0 - h / 2f - 0.005f / 2), 0);

    FixtureDef fxtDefSensorCap = new FixtureDef();
    // fxtDefSensorCap.isSensor = true;
    fxtDefSensorCap.shape = sensorCapShape;

    Fixture sensorCapFxt = body.createFixture(fxtDefSensorCap);
    sensorCapFxt.setUserData("sensorCapToad");

    // log().info("Massa de toad: " + String.valueOf(body.getMass()));

    // init prevX, prexY, prevA
    prevX = body.getPosition().x;
    prevY = body.getPosition().y;
    prevA = body.getAngle();

    // Útil per gestió de colisions
    body.setUserData(this);
  }


  // public void paint(float alpha) {
  // float x, y, a;
  // x = body.getPosition().x;
  // y = body.getPosition().y;
  // a = body.getAngle();
  //
  // sprite.layer().setTranslation(pfcjoc.toPix(x), pfcjoc.toPix(y));
  // sprite.layer().setRotation(a);
  // }

  public void update(float delta) {
    super.update(delta);

    if (numEsqContacts > 0) {
      movingRight = true;
      movingLeft = false;
    }
    else if (numDretaContacts > 0) {
      movingRight = false;
      movingLeft = true;
    }
    if (movingLeft) {
      body.setLinearVelocity(new Vec2(-MAX_SPEED, body.getLinearVelocity().y));
    }
    else if (movingRight) {
      body.setLinearVelocity(new Vec2(MAX_SPEED, body.getLinearVelocity().y));
    }

    // Sprites
    if (timeSinceLastSprite > 4) {
      timeSinceLastSprite = 0;
      spriteIndex = (spriteIndex + 1) % numSpritesRunAnim;
      sprite.setSprite(spriteIndex);
    }
    timeSinceLastSprite += 1;
  }


  public void setNumEsqContacts(int nec) {
    numEsqContacts = nec;
  }


  public int getNumEsqContacts() {
    return numEsqContacts;
  }


  public void setNumDretaContacts(int ndc) {
    numDretaContacts = ndc;
  }


  public int getNumDretaContacts() {
    return numDretaContacts;
  }


  public Body getBody() {
    return body;
  }


  public Layer getLayer() {
    return sprite.layer();
  }


  public String getName() {
    return name;
  }


  @Override
  public float getWidth() {
    return 32;
  }


  @Override
  public float getHeight() {
    return 32;
  }


  @Override
  protected int getFirstSprite() {
    return 0;
  }


  @Override
  protected String getImagePath() {
    return "images/toad.png";
  }


  @Override
  protected String getJsonPath() {
    return "images/toad.json";
  }


  public Vec2 getPosition() {
    return position;
  }

}
